var app = getApp()
var util = require('md5.js');
Page({
  data: {
    scene: '',
    userName: '',
    userPsw: '',
    member:'',
  },
  onLoad: function (options) {
    console.log(options)
    var that = this
    var scene_img = 'https://xiaochengxu-ajihua.oss-cn-hangzhou.aliyuncs.com/app/down.png' //这里添加图片的地址
    that.setData({
      scene: scene_img
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      urls: this.data.scene.split(',')
    })
  }, 
  gobannerUrl: function () {
    wx.navigateTo({
      url: '../index/page/bannerUrl/bannerUrl',
    })
  },
  q: function () {
    wx.navigateBack({ changed: true })
  },
  qq: function () {
    wx.switchTab({

      url: '/pages/index/index',

    });
  },
  onReady: function () {
    const vm = this
    vm.setData({
      statusBarHeight: getApp().globalData.statusBarHeight,
      titleBarHeight: getApp().globalData.titleBarHeight
    })

  },

   onShow: function () {
     console.log('onShow')
     var that = this

     if (app.globalData.myId == null || app.globalData.myId == undefined || app.globalData.myId == '') {
       wx.navigateTo({
         url: '../login/login',
       })
     } else {
      
       if (app.globalData.member == null || app.globalData.myId == undefined || app.globalData.member == ''){
         this.getInfo()
       }else{
         this.setData({
           member: app.globalData.member
         })
         if (app.globalData.isLoginPage) {  

           wx.switchTab({
             url: '../index/index',
           })
          
          
           app.globalData.isLoginPage = false
         } else {

       }
     }
     }
   },
 
  getInfo:function(){
    wx.showLoading({
      title: '请稍候',
    })
    var that = this
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/MemberXcx/index/',
      data: {
        myId: app.globalData.myId,

      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        if (res.data.status === 1) {
          that.setData({
            member: res.data.member
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '获取失败' + res.data.Message,
            showCancel: false
          })
        }
      },
      fail: function (e) {
        wx.hideLoading()
      }

    })
  },

  getOid: function () {
    var that = this
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/OrderXcx/index/',
      data: {
        myId: app.globalData.myId,

      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)

        if (res.data.status === 1) {
          that.getPrePayId( res.data.oid)

        }
        else {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '请求失败',
            showCancel: false
          })
        }
      }, fail: function (e) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '请求失败',
          showCancel: false
        })
      }

    })
  },

  getPrePayId:function(oid){
    var that = this
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/WeiPayXcx/prePayProcess/',
      data: {
        oid: oid,
        myId: app.globalData.myId,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)

        if (res.data.status === 1) {
          that.sendPayMent(res.data.nonce_str, res.data.prepay_id)

        }
        else {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '请求失败',
            showCancel: false
          })
        }
      }, fail: function (e) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '请求失败',
          showCancel: false
        })
      }

    })
  },

  sendPayMent: function (nonce_str, prepay_id) {
    var that = this
    var time = Date.parse(new Date()) / 1000;
    var paySign = util.hexMD5('appId=wxd59c11f9decb5e58&nonceStr=' + nonce_str + '&package=prepay_id=' + prepay_id + '&signType=MD5&timeStamp=' + time + '&key=lkjmkluhjjm3256jkk52188iojkklok5')

    wx.requestPayment({
      'timeStamp': time + '',
      'nonceStr': nonce_str,
      'package': 'prepay_id=' + prepay_id,
      'signType': 'MD5',
      'paySign': paySign,
      success: function (res) {
        console.log(res)

        if (res.errMsg == 'requestPayment:ok') {
   
          wx.showToast({
            title: '支付成功',
          })
          that.data.member.type=5
          that.setData({
            member: that.data.member
          })
        } else {

        }
  
      },
      'fail': function (res) {
        console.log('fail')
        console.log(res)
      }
    })
  },
})